﻿using NotesVaultOnline.Data.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace NotesVaultOnline.Controllers
{
    public class UserController : Controller
    {
        /// <summary>
        /// Render login page
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ViewResult SignIn()
        {
            return View(new UserSignIn());
        }

        /// <summary>
        /// Action login request
        /// </summary>
        /// <param name="Email"></param>
        /// <param name="Pass"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult SignIn(UserSignIn model)
        {
            model.Result = new CustomResult { Type = CustomResult.ResultType.Success, Message = "Hello World!" };
            string foundUser = Membership.GetUserNameByEmail(model.Email);
            if (string.IsNullOrEmpty(foundUser))
            {
                model.Result.Type = CustomResult.ResultType.Error;
                model.Result.Message = "This email is not registered to our system.";
            }
            else
            {
                if (Membership.ValidateUser(foundUser, model.Password))
                {
                    model.Result.Message = string.Format("Hi, {0}!", foundUser);
                    FormsAuthentication.SetAuthCookie(foundUser, true);
                    //Sign in succeeds
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    model.Result.Type = CustomResult.ResultType.Error;
                    model.Result.Message = "Invalid Email and Password combination.";
                }
            }

            //Sign in fails
            return View(model);
        }

        [Authorize]
        public ActionResult SignOut()
        {
            FormsAuthentication.SignOut();
            return RedirectToAction("Index", "Home");
        }

        [HttpGet]
        [Authorize]
        public ViewResult Settings()
        {
            return View("UnderConstruction");
        }

        [HttpGet]
        public ViewResult Forgot()
        {
            return View("UnderConstruction");
        }

        [HttpGet]
        public ViewResult Register()
        {
            //Default tick auto login
            UserRegistration registration = new UserRegistration();
            registration.AutoLogin = true;
            
            //TODO: Debug version
            return View("UnderConstruction");

            //return View(registration);
        }

        [HttpPost]
        public ActionResult Register(UserRegistration model)
        {
            MembershipCreateStatus status = MembershipCreateStatus.Success;
            model.Result = new CustomResult { Type = CustomResult.ResultType.Success, Message = status.ToString() };
            Membership.CreateUser(model.UserName, model.Password, model.Email, null, null, true, out status);
            if (status != MembershipCreateStatus.Success)
            {
                model.Result.Type = CustomResult.ResultType.Error;
                model.Result.Message = status.ToString();

            }
            else
            {
                if (model.AutoLogin)
                {
                    FormsAuthentication.SetAuthCookie(model.UserName, true);
                    return RedirectToAction("Index", "Home");
                }
            }

            //Registration fails
            return View(model);
        }
    }
}
